/* 
 * Author: Alexander Rauser, arauser.org
 * Date: August 2008
 * Copyright: 2008
 * Licensed under Creative Commons Attribution 3.0 Unported
 */
 
package org.arauser.uploader.view
{
	import flash.desktop.*;
	import flash.events.*;
	
	import mx.managers.PopUpManager;
	

	import org.arauser.uploader.view.components.Configuration;
	import org.arauser.uploader.model.SettingsProxy;
	import org.puremvc.as3.interfaces.*;
	import org.puremvc.as3.patterns.facade.*;
	import org.puremvc.as3.patterns.mediator.*;
	/**
	 * The mediator for the Configuration screen.
	 * Shows all editable settings and saves them using the settings Proxy.
	 */
	public class ConfigurationMediator extends Mediator implements IMediator
	{
		// Cannonical name of the Mediator
        public static const NAME:String = 'ConfigurationMediator';
 		private var proxy:SettingsProxy;
            
        /**
        * Constructor
        */ 
        public function ConfigurationMediator(viewComponent:Configuration) {
            
            super( NAME, viewComponent );
            
            proxy = SettingsProxy(facade.retrieveProxy(SettingsProxy.NAME));
            
            // Listen for events from the view component
            viewComponent.addEventListener("SAVE", onSave);
            viewComponent.addEventListener("CANCEL", onCancel);
            
            configuration.uploadURL.text = proxy.settings["uploadURL"];
        }
 
 		/**
 		 * Saves the settings and removes the view
 		 */
		private function onSave(event:MouseEvent):void
		{
			proxy.saveSetting("uploadURL", configuration.uploadURL.text);
            PopUpManager.removePopUp(configuration);
        }
		
		/**
		 * Handler for the cancel button, removes the view
		 */
		private function onCancel(event:MouseEvent):void
		{
			PopUpManager.removePopUp(configuration);
        }

        /**
        * Return list of Nofitication names that Mediator is interested in
        */
        override public function listNotificationInterests():Array {
            return [];
        }
 
        /**
        * Handle all notifications this Mediator is interested in
        */
        override public function handleNotification(note:INotification):void  {
            /*
            switch (note.getName()) {
                case ApplicationFacade.STARTUP:
                    break;
            }
            */
        }
 
        /**
        * Cast the viewComponent to its actual type.
        */
		protected function get configuration() : Configuration {
			return viewComponent as Configuration;
		}
	}
}